import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

//@Composable
//fun MyDisposableEffect1(){
//    Column {
//        var count by remember { mutableStateOf(1) }
//        Button(onClick = { count += 1 }) {
//            Text(text = "切换 $count")
//        }
//        if (count<3) {
//            Text(text = "啦啦啦啦")
//            DisposableEffect(Unit) {
//                // 进入界面的回调
//                println("显示文本")
//                onDispose {
//                    // 离开界面的回调
//                    println("隐藏文本")
//                }
//            }
//        }
//    }
//
//}

@Composable
fun TestDisposableEffect2(clicked: MutableState<Int>) {
    println("disposable effect before: ${clicked.value}")
    DisposableEffect(key1 = clicked.value) {
        println( "disposable effect called")
        onDispose {
            println( "disposable effect disposed")
        }
    }
    println("disposable effect after: ${clicked.value}")
    Column {
        Text(
            text = "Clicked: ${clicked.value}", modifier = Modifier
                .padding(16.dp)
                .clickable {
                    clicked.value += 1
                }
        )
    }
    println("Composable end")
}

@Composable
fun TestDisposableEffect3(clicked: MutableState<Int>) {
    println("disposable effect before: ${clicked.value}")
    DisposableEffect(key1 = Unit) {
        println( "disposable effect called")
        onDispose {
            println( "disposable effect disposed")
        }
    }
    println("disposable effect after: ${clicked.value}")
    Column {
        Text(
            text = "Clicked: ${clicked.value}", modifier = Modifier
                .padding(16.dp)
                .clickable {
                    clicked.value += 1
                }
        )
    }
    println("Composable end")
}